package com.capacitydorm.server.service;

import com.capacitydorm.server.common.vo.SelectAllDormVo;
import com.capacitydorm.server.common.vo.SelectAllFloorVo;
import com.capacitydorm.server.common.vo.SelectAllStuVo;
import com.capacitydorm.server.common.vo.SelectAllTowerVo;
import com.capacitydorm.server.dao.DormMapper;
import com.capacitydorm.server.dao.FloorMapper;
import com.capacitydorm.server.dao.StudentMapper;
import com.capacitydorm.server.dao.TowerMapper;
import com.capacitydorm.server.domain.Dorm;
import com.capacitydorm.server.domain.Floor;
import com.capacitydorm.server.domain.Student;
import com.capacitydorm.server.domain.Tower;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class TowerService {
    @Resource
    private TowerMapper towerMapper;

    @Resource
    private FloorMapper floorMapper;

    @Resource
    private DormMapper dormMapper;

    @Resource
    private StudentMapper studentMapper;

    public List<SelectAllTowerVo> getAllStudent() {
        List<SelectAllTowerVo> selectAllTowerVos = new ArrayList<>();

        List<Tower> towers = towerMapper.selectAll();
        for (Tower tower : towers) {

            //注入tower
            SelectAllTowerVo selectAllTowerVo = new SelectAllTowerVo(tower.getId(), tower.getName());

            //注入floor
            List<SelectAllFloorVo> selectAllFloorVos = new ArrayList<>();
            List<Floor> floors = floorMapper.findByTowerId(tower.getId());
            for (Floor floor : floors) {
                SelectAllFloorVo selectAllFloorVo = new SelectAllFloorVo(floor.getId(), floor.getNumber(), floor.getTowerId());

                //注入dorm
                List<SelectAllDormVo> selectAllDormVos = new ArrayList<>();
                List<Dorm> dormList = dormMapper.findByFloorId(floor.getId());
                for (Dorm dorm : dormList) {
                    SelectAllDormVo selectAllDormVo = new SelectAllDormVo(dorm.getId(), dorm.getNumber(), dorm.getFloorId());

                    //注入学生
                    List<SelectAllStuVo> selectAllStuVos = new ArrayList<>();
                    List<Student> students = studentMapper.findByDormId(dorm.getId());
                    for (Student student : students) {
                        SelectAllStuVo selectAllStuVo = new SelectAllStuVo(student.getId(), student.getName(), student.getClasses(), student.getSex(), student.getPhone(), student.getState(), student.getLastout(), student.getLastin(), student.getWxuserId(), student.getDormId(), student.getReason());
                        selectAllStuVos.add(selectAllStuVo);
                    }

                    selectAllDormVo.setStudentList(selectAllStuVos);

                    selectAllDormVos.add(selectAllDormVo);
                }

                selectAllFloorVo.setDormList(selectAllDormVos);
                selectAllFloorVos.add(selectAllFloorVo);
            }

            selectAllTowerVo.setFloorList(selectAllFloorVos);
            selectAllTowerVos.add(selectAllTowerVo);
        }

        return selectAllTowerVos;
    }
}
